package com.imax.center.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.imax.center.common.Result;
import com.imax.center.service.StatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Api(tags = "H3yun统计接口")
@RestController
@RequestMapping("/api/statistics")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    @ApiOperation(value = "统计基础数据", notes = "统计基础数据")
    @GetMapping("/getBaseCount")
    public Result<JSONObject> getBaseCount(String areaCode) {
        return statisticsService.getBaseCount(areaCode);
    }

    @ApiOperation(value = "统计监控点在线、离线数量", notes = "统计监控点在线、离线数量")
    @GetMapping("/getCameraOnline")
    public Result<JSONObject> getCameraOnline(String areaCode) {
        return statisticsService.getCameraOnline(areaCode);
    }

    @ApiOperation(value = "统计单位数量", notes = "统计单位数量")
    @GetMapping("/getOrgStatistics")
    public Result<JSONArray> getOrgStatistics(String areaCode) {
        return statisticsService.getOrgStatistics(areaCode);
    }

    @ApiOperation(value = "统计单位数量-地图", notes = "统计单位数量-地图")
    @GetMapping("/getOrgMapStatistics")
    public Result<JSONObject> getOrgMapStatistics(String areaCode) {
        return statisticsService.getOrgMapStatistics(areaCode);
    }

    @ApiOperation(value = "统计事件数据", notes = "统计事件数据")
    @PostMapping("/getEventCount")
    public Result<JSONObject> getEventCount(@RequestBody JSONObject params) {
        return statisticsService.getEventCount(params);
    }

    @ApiOperation(value = "统计近7天超标预警趋势", notes = "统计近7天超标预警趋势")
    @GetMapping("/get7DayAlarmCount")
    public Result<JSONArray> get7DayAlarmCount(String areaCode) {
        return statisticsService.get7DayAlarmCount(areaCode);
    }

    @ApiOperation(value = "统计近1年污水流量趋势", notes = "统计近1年污水流量趋势")
    @GetMapping("/get1YearW00000RtdTrend")
    public Result<JSONArray> get1YearW00000RtdTrend(String areaCode) {
        return statisticsService.get1YearW00000RtdTrend(areaCode);
    }

    @ApiOperation(value = "统计近7天异常超标趋势数据", notes = "统计近7天异常超标趋势数据")
    @GetMapping("/get7DayTrendCount")
    public Result<JSONArray> get7DayTrendCount(String areaCode) {
        return statisticsService.get7DayTrendCount(areaCode);
    }

    @ApiOperation(value = "统计近7天处置趋势", notes = "统计近7天处置趋势")
    @GetMapping("/get7DayHandlerTrend")
    public Result<JSONArray> get7DayHandlerTrend(String areaCode) {
        return statisticsService.get7DayHandlerTrend(areaCode);
    }

    @ApiOperation(value = "获取近8h数据上报趋势", notes = "获取近8h数据上报趋势")
    @GetMapping("/getDataUploadTrend")
    public Result<JSONArray> getDataUploadTrend(String areaCode) {
        return statisticsService.getDataUploadTrend(areaCode);
    }

    @ApiOperation(value = "统计污水处理相关数据", notes = "统计污水处理相关数据")
    @GetMapping("/statW00000CountInfo")
    public Result<JSONObject> statW00000CountInfo(String areaCode) {
        return statisticsService.statW00000CountInfo(areaCode);
    }

    @ApiOperation(value = "统计药剂消耗相关数据", notes = "统计药剂消耗相关数据")
    @GetMapping("/statDisinfectantCountInfo")
    public Result<JSONObject> statDisinfectantCountInfo(String areaCode) {
        return statisticsService.statDisinfectantCountInfo(areaCode);
    }

    @ApiOperation(value = "统计危险废物相关数据", notes = "统计危险废物相关数据")
    @GetMapping("/statSludgeCountInfo")
    public Result<JSONObject> statSludgeCountInfo(String areaCode) {
        return statisticsService.statSludgeCountInfo(areaCode);
    }

    @ApiOperation(value = "统计能耗相关数据", notes = "统计能耗相关数据")
    @GetMapping("/statElectricityCountInfo")
    public Result<JSONObject> statElectricityCountInfo(String areaCode) {
        return statisticsService.statElectricityCountInfo(areaCode);
    }

    @ApiOperation(value = "获取医院传感器数据", notes = "获取医院传感器数据")
    @PostMapping("/getHospitalSensorData")
    public Result<JSONArray> getHospitalSensorData(@RequestBody JSONObject params) {
        return statisticsService.getHospitalSensorData(params);
    }

    @ApiOperation(value = "导出医院传感器数据", notes = "导出医院传感器数据到Excel文件")
    @PostMapping("/exportHospitalSensorData")
    public void exportHospitalSensorData(@RequestBody JSONObject params, HttpServletResponse response) {
        try {
            statisticsService.exportHospitalSensorData(params, response);
        } catch (IOException e) {
            log.error("导出医院传感器数据失败", e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

} 